package demo.join;

/**
 * Version: 1.1
 * Author: TangJun
 * Date: 2024-07-25
 */
public class Join {
    public static void main(String[] args) {
        Boss boss = new Boss();
        Thread t = new Thread(boss);
        t.start();

        int count = 0;
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(500);
                System.out.println(Thread.currentThread().getName() + (++count));
                if (count == 5) {
                    t.join();
                }
            } catch (InterruptedException e) {
                System.out.println("主线程中断");
            }
        }
    }
}

class Boss implements Runnable{
    private int count;
    private boolean loop = true;

    public void setLoop(boolean loop) {
        this.loop = loop;
    }

    @Override
    public void run() {
        while (loop) {
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + (++count));
            } catch (InterruptedException e) {
                System.out.println("Boss子线程中断");
            }
            if (count >= 10) {
                System.out.println("Boss子线程执行完毕");
                break;
            }
        }
    }
}
